<?php

namespace App\Http\Controllers;

use App\Models\SystemConfigType;
use Illuminate\Http\Request;

class SystemConfigTypeController extends Controller
{
    /**
     * 系统配置类型列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(): \Illuminate\Http\JsonResponse
    {
        return response()->json(['data' => SystemConfigType::all()]);
    }

    /**
     * 查看系统配置类型详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id): \Illuminate\Http\JsonResponse
    {
        $systemConfigType = SystemConfigType::find($id);
        return response()->json(['data' => $systemConfigType]);
    }

    /**
     * 新增系统配置类型
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request): \Illuminate\Http\JsonResponse
    {
        $data = $request->only(['name', 'description']);
        SystemConfigType::create($data);
        return response()->json(['message' => '新增系统配置类型成功']);
    }

    /**
     * 更新系统配置类型
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, $id): \Illuminate\Http\JsonResponse
    {
        $systemConfigType = SystemConfigType::find($id);
        $systemConfigType->update($request->only(['name', 'description']));
        return response()->json(['message' => '更新系统配置类型成功']);
    }

    /**
     * 删除系统配置类型
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id): \Illuminate\Http\JsonResponse
    {
        if (SystemConfigType::find($id)->systemConfigs()->count() > 0) {
            return response()->json(['message' => '该系统配置类型下存在系统配置，无法删除'], 400);
        }
        SystemConfigType::destroy($id);
        return response()->json(['message' => '删除系统配置类型成功']);
    }
}
